home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d1
/
batchlrn.arc
/
FORINDO.HLP
< prev
next >
Wrap
Text File
|
1991-06-17
|
7KB
|
91 lines
|---------------B A T C H L R N H E L P S Y S T E M-----------------|
|command: FOR..IN..DO (Actually multiple commands) |
|use:FOR..IN..DO string is used to expand the power of a batch file |
| or as an interactive (from the keyboard prompt) command |
| |
|how: In Batch processing type: FOR %%<c>IN<set>DO<command> |
| In Interactive processing type: FOR %<c>IN<set>DO<command> |
| |
|explanation: The FOR command is similar to the "for..next loop" that |
| programmers use to repeat a paricular action a number of times. A |
| batch file to compare files on A:& B: drives looks like this: |
| ECHO OFF [keeps command clutter of the screen] |
| CLS [we always do this to get ECHO OFF removed] |
| FOR %%Y IN (*.*) DO IF EXIST B:%%Y ECHO %%Y IS ON BOTH A:&B: |
| | | |=the<command>(here, starting on A:drive,the |
| | | DO sees if X {which is *.*[any of all the |
| | | files on A:]} exists on B: the ECHOes the |
| | | file name {X} as being on "BOTH A:&B:") |
| | | |
| | |=IN<set> the set is *.* or all the files on A:&B: |
| | |
| |=FOR is seeking the variable %X (it could be any letter) |
| |
| |
|examples: FOR %%gf IN(*.ASM)DO MASM %%f; |
| FOR %%f IN(BAK ART BUDGT) DO REM %%f |
| The "%%" is needed so that after batch parameter (%0-%9) processing |
| is complete, a % remains. If only %f was entered, the DOS batch pa- |
| rameter processor sees the first "%", looks at "f", then decides |
| that "%f" is a bad parameter reference, and discards the "%f". |
| The FOR command would never receive this parameter. In a batch |
| file, you must use the expression "%%". |
| Let's see hoe the batch command works... The first line turns |
| command displat off to clear command clutter. The second line clears|
| the "echo off" messagefrom the screen. The third line is executed |
| many times as there are files on the disk in a: [the set (*.*) |
| assures this]. Each of those filenamesare assigned to %%Z in turn |
| and then checked for presence on drive B: with the EXIST logical |
| statement. If EXIST is true, then the message at the end of the IF |
| subcommand si sent to the screen, otherwise nothing is printed and |
| the next file on driove A: is assigned and checked. |
| FILES on drive A: FILES on drive B: |
| ----------------- ------------------ |
| COMMAND.COM COMMAND.COM |
| FILE.ONE FILE.ONE |
| FILE.TWO FILE.LTR |
| The batch subcommand we are investigating is: |
| FOR %%Z IN (*.*) DO IF EXIST B:%%Z is on both A: and B: |
| Each filename on A: is substitutedin the IF subcommand and then |
| executed. To get the same effect you would have to type-- |
| IF EXIST B:COMMAND.COM ECHO COMMAND.COM is on both A: and B: |
| IF EXIST B:FILE.ONE ECHO FILE.ONE is on both A: and B: |
| IF EXIST B:FILE.TWO ECHO FILE.TWO is on both A: and B: |
| In the case of the example above, the first two would have a posi- |
| tive responseand the last would not print anything. Study it care- |
| fully before going on. |
| FILES on drive A: FILES on drive B: |
| ----------------- ----------------- |
| COMMAND.COM COMMAND.COM |
| FILE.ONE FILE.ONE |
| FILE.TWO FILE.LTR |
| OK, told you to studt the example. Let's see if you remember. What |
| is the one line batch file command to find and report out all files |
| starting with an "F" on drive A:? [I'll help you a little, you fill |
| in the blanks.] |
| ___%%Z IN (A:_____) ____ECHO File_____is on drive A: |
| FOR %%Z IN (A:F*.*) DO ECHO File %%Z is on drive A: |
| In this case you see that the A: disk is checked for any file start-|
| ing with the letter "F" and that name is substituted in the varible |
| %%Z. The appropriate message is then printed with the proper file- |
| name. |
| This is not an easy concept. Let's now look at using DOS commands |
| in (Set). |
| FOR...IN..DO using DOS Commands |
| ================================ |
| The set can contain DOS commands instead of filenames and these |
| commands will then be executed in sequence (to include running |
| large programes under the control of the FOR..IN..DO loop). |
| Let's say you want to sequentially: Clear the screen |
| Show the DOS version number |
| then Show the disk directory with the pause on |
| You could do all that in a one line batch file with the following |
| command in it:: FOR %%T IN (CLS VER DIR/P) DO %%T |
| When using DOS commands in (SET) ypu must use the space as a de- |
| limiter and cannot have asterisk (*) or question mark (?) in any |
| command. Use a colon(:) instead of a space when passing parameters |
| to programs (i.e. DBASE:FILE INSTEAD OF DBASE FILE) |
| It is possible to issue the FOR..IN..DO command at the DOS prompt |
| by dropping one of the percentage signs (%) an the varible. |
|----------------- T I M E M A S T E R ---------------------|